package com.xuhuan.demo.collection;

import com.xuhuan.demo.collection.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 徐欢
 * @date: 2021/7/27 19:00
 * @description: 集合工具类
 */
@Slf4j
public class CollectionUtilsDemo {

    private List<User> list1;
    private List<User> list2;

    /**
     * 准备集合数据
     */
    @Before
    public void handleData() {
        list1 = Arrays.asList(
                new User("name1", 11),
                new User("name2", 12),
                new User("name3", 13),
                new User("name4", 14)
        );

        list2 = Arrays.asList(
                new User("name2", 12),
                new User("name3", 13),
                new User("name4", 14),
                new User("name5", 15),
                new User("name6", 16)
        );
    }

    /**
     * 用来判断空
     */
    @Test
    public void testIsEmpty() {
        log.info("CollectionUtils.isEmpty(null)：结果[{}]", CollectionUtils.isEmpty(null));//true
        log.info("CollectionUtils.isEmpty(new ArrayList())：结果[{}]", CollectionUtils.isEmpty(new ArrayList()));//true
    }

    /**
     * 判断是否不为空
     */
    @Test
    public void testIsNotEmpty() {
        log.info("CollectionUtils.isNotEmpty(null)：结果[{}]", CollectionUtils.isNotEmpty(null));//false
        log.info("CollectionUtils.isNotEmpty(new ArrayList())：结果[{}]", CollectionUtils.isNotEmpty(new ArrayList()));//false
    }

    /**
     * 并集
     */
    @Test
    public void testUnion() {
        String[] arrayA = new String[]{"A", "B", "C", "D", "E", "F"};
        String[] arrayB = new String[]{"B", "D", "F", "G", "H", "K"};
        List listA = Arrays.asList(arrayA);
        List listB = Arrays.asList(arrayB);
        log.info("并集：[{}]", CollectionUtils.union(listA, listB));

        log.info("对象并集：[{}]", CollectionUtils.union(list1, list2));
    }

    /**
     * 交集
     */
    @Test
    public void testIntersection() {
        String[] arrayA = new String[]{"A", "B", "C", "D", "E", "F"};
        String[] arrayB = new String[]{"B", "D", "F", "G", "H", "K"};
        List listA = Arrays.asList(arrayA);
        List listB = Arrays.asList(arrayB);
        log.info("交集：[{}]", CollectionUtils.intersection(listA, listB));

        log.info("对象交集：[{}]", CollectionUtils.intersection(list1, list2));
    }

    /**
     * 两个list<user>的交集
     * 需要重写对象的equals和hashCode方法
     */
    @Test
    public void testIntersectionObject() {
        User[] arrayA = new User[]{new User("a", 1), new User("b", 2)};
        User[] arrayB = new User[]{new User("a", 1), new User("c", 3)};
        List<User> listA = Arrays.asList(arrayA);
        List<User> listB = Arrays.asList(arrayB);

        log.info("对象的交集：[{}]", CollectionUtils.intersection(listA, listB));//[User{name='a', age=1}]

    }

    /**
     * 交集的补集
     */
    @Test
    public void testDisjunction() {
        String[] arrayA = new String[]{"A", "B", "C", "D", "E", "F"};
        String[] arrayB = new String[]{"B", "D", "F", "G", "H", "K"};
        List listA = Arrays.asList(arrayA);
        List listB = Arrays.asList(arrayB);
        log.info("交集的补集：[{}]", CollectionUtils.disjunction(listA, listB));
    }

    /**
     * 差集
     */
    @Test
    public void testSubtract() {
        String[] arrayA = new String[]{"A", "B", "C", "D", "E", "F"};
        String[] arrayB = new String[]{"B", "D", "F", "G", "H", "K"};
        List listA = Arrays.asList(arrayA);
        List listB = Arrays.asList(arrayB);
        //arrayA扣除arrayB
        log.info("差集：[{}]", CollectionUtils.subtract(listA, listB));

        log.info("对象的差集：[{}]", CollectionUtils.subtract(list1, list2));

    }

    @Test
    public void testEqual() {
        String a = "hello";
        String b = "hello";
        log.info("a==b :{}", a == b);
        log.info("a.quals(b) :{}", a.equals(b));

        Object o1 = new Object();
        Object o2 = new Object();
        Object o3 = o2;
        log.info("o1==o2：{}", o1 == o2);
        log.info("o1.equals(o2)：{}", o1.equals(o2));

        log.info("o2==o3：{}", o2 == o3);
        log.info("o2.equals(o3)：{}", o2.equals(o3));
    }
}
